package com.example.beautyimage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

public class PrimaryColor extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private ImageView imageView;
    private SeekBar skbHue;
    private SeekBar skbSaturation;
    private SeekBar skbLum;
    private Bitmap mBitmap;

    //初始化seekbar显示值大小
    private static int MAX_VALUE = 255;
    private static int MID_VALUE = 128;
    private float mHue, mSaturation, mLum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary_color);
        //把图片转化为Bitmap
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bqb);
        imageView = findViewById(R.id.imageview);
        skbHue = findViewById(R.id.skb_Hue);
        skbSaturation = findViewById(R.id.skb_saturation);
        skbLum = findViewById(R.id.skb_lum);

        skbHue.setOnSeekBarChangeListener(this);
        skbSaturation.setOnSeekBarChangeListener(this);
        skbLum.setOnSeekBarChangeListener(this);

        //SeekBar的最大显示值
        skbHue.setMax(MAX_VALUE);
        skbSaturation.setMax(MAX_VALUE);
        skbLum.setMax(MAX_VALUE);
        //SeekBar的初始显示位置
        skbHue.setProgress(MID_VALUE);
        skbSaturation.setProgress(MID_VALUE);
        skbLum.setProgress(MID_VALUE);

        imageView.setImageBitmap(mBitmap);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //对图片进行美图动作
        switch (seekBar.getId()) {
            case R.id.skb_Hue:
                mHue = (progress - MID_VALUE) * 1.0f / MID_VALUE * 180;
                break;
            case R.id.skb_saturation:
                mSaturation = progress * 1.0f / MID_VALUE;
                break;
            case R.id.skb_lum:
                mLum = progress * 1.0f / MID_VALUE;
                break;
        }
        imageView.setImageBitmap(ImageHelper.handleImageEffect(mBitmap, mHue, mSaturation, mLum));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
